#pragma once

#include "Vector.h"

Vector::Vector(size_t size)
{
   len = size;
   if (len == 0)
   {
      digit = 0;
   }
   else
   {
      digits = new int64[len];
      for (size_t i = 0; i < len; i++)
         digits[i] = 0;
   }
}

Vector::Vector(const Vector& other)
{
   if (&other == this)
      return;
   len = other.size();
   if (len == 1)
      digit = other[0];
   if (other.size() > 1)
   {
      digits = new int64[len];
      for (size_t i = 0; i < len; i++)
         digits[i] = other[i];
   }
}

Vector& Vector::operator=(const Vector& other)
{
   if (&other == this) return *this;
   if (len > 1)
      delete[] digits;
   len = other.size();
   if (len == 1)
      digit = other.digit;
   if (len > 1)
   {
      digits = new int64[len];
      for (size_t i = 0; i < len; i++)
         digits[i] = other[i];
   }
   return *this;
}

size_t Vector::size() const
{
   return len;
}

int64& Vector::operator[](size_t index)
{
   if (index >= len)
      resize(index + 1);
   if (len == 1)
      return digit;
   else
      return digits[index];
}

int64 Vector::operator[](size_t index) const
{
   if (index >= len)
      return 0;
   if (len == 1)
      return digit;
   else
      return digits[index];
}

Vector::~Vector()
{
   if (len > 1)
      delete[] digits;
}

void Vector::resize(size_t new_size)
{
   if (new_size < len)
      return;
   if (len * 2 >= new_size)
      new_size = len * 2;
   Vector tmp(new_size);
   if (len == 1)
      tmp[0] = digit;
   else
      for (size_t i = 0; i < len; i++)
         tmp[i] = digits[i];
   for (size_t i = len; i < new_size; i++)
      tmp[i] = 0;
   *this = tmp;
}

void Vector::swap(Vector &other)
{
   std::swap(len, other.len);
   std::swap(digit, other.digit);
}